package com.example.jegotrip.fragment.search;

import static android.view.View.GONE;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.adapter.NoteListAdapter;
import com.example.jegotrip.base.BaseFragment;
import com.example.jegotrip.db.NoteDBHelper;
import com.example.jegotrip.entity.NoteInfo;
import com.example.jegotrip.utils.CustomToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SearchFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private TextView ev_search;
    private EditText ev_search_keyword;
    //    适配器
    private NoteListAdapter noteListAdapter;

    private LinearLayoutCompat ll_search_empty;

    private List<NoteInfo> noteInfos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_search, container, false);
        return rootView;
    }

    @Override
    protected void initData() {

        // 初始化适配器
        noteListAdapter = new NoteListAdapter(noteInfos);
        // 设置适配器
        recyclerView.setAdapter(noteListAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()){
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        };

        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initViews() {
        recyclerView = rootView.findViewById(R.id.search_recyclerView);

        ll_search_empty = rootView.findViewById(R.id.ll_search_empty);
        ev_search_keyword = rootView.findViewById(R.id.ev_search_keyword);
        ev_search = rootView.findViewById(R.id.ev_search);
        ev_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = ev_search_keyword.getText().toString();
                if (TextUtils.isEmpty(content)){
                    CustomToast.showCenterToast(requireActivity(),"请输入搜索内容");
                } else {
                    noteInfos = NoteDBHelper.getInstance(requireActivity()).searchNotes(content);
                    if (noteListAdapter != null){// 对数据进行赋值展示
                        noteListAdapter.setNoteInfos(noteInfos);
                    }

                    if (noteInfos.isEmpty()){
                        ll_search_empty.setVisibility(View.VISIBLE);
                    } else {
                        ll_search_empty.setVisibility(GONE);
                    }
                }
            }
        });
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.fragment_search;
    }
}


